package com.wn.user.controller;

import com.wn.common.entity.*;
import com.wn.common.pojo.User;;
import com.wn.user.param.PropertyParam;
import com.wn.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @BelongsProject: shared_parking
 * @BelongsPackage: com.wn.user.controller.reception
 * @Author: 廖刚
 * @CreateTime: 2020-06-11 14:27
 * @Description: 前台用户表现层
 */
@Api(value = "User模块接口",description = "这是一个用户模块的接口文档")
@RestController
public class UserController {

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;




    @ApiOperation("用户登录")
    @PostMapping("login")
    public MyRsp login(@RequestBody User user,HttpServletResponse response){

        logger.info("**:{}",user);

        return userService.login(user,response);

    }

    @ApiOperation("添加一个用户")
    @PostMapping("addUser")
    public MyRsp addUser(User user){
        user.setUId(UUID.randomUUID().toString());
        user.setCreateUserId(user.getUId());
        user.setUpdateUserId(user.getUId());
        return userService.addUser(user) > 0 ? MyRsp.success("添加一个用户成功"):MyRsp.error().msg("添加用户失败");
    }

    @ApiOperation("查询所有用户 支持多条件分页排序查询")
    @PostMapping("searchSomeUserByPage")
    public MyRsp searchSomeUserByPage(@RequestBody MyParam<User> param){

        return MyRsp.success(userService.getAllUser(param)).msg("查询成功");
    }



    @ApiOperation("根据id获取用户对象")
    @PostMapping("getUserById")
    public MyRsp getUserById(@RequestBody String uId){
        User user = userService.getUserById(uId);
        return user != null ? MyRsp.success(user).msg("查询成功"):MyRsp.error().msg("查询失败");
    }

    @ApiOperation("通过用户名判断是否存在这样的用户")
    @PostMapping("existUserByUsername")
    public MyRsp existUserByUsername(String username){
        boolean b = userService.existUserByUsername(username);
        return b ? MyRsp.success(b).msg("存在此用户名的用户"):MyRsp.error().msg("不存在此用户名的用户");
    }

    @ApiOperation("通过id物理删除一个用户对象")
    @PostMapping("deleteUserTrueById")
    public MyRsp deleteUserTrueById(String uId){
        return userService.deleteUserTrueById(uId) > 0?MyRsp.success("物理删除成功"):MyRsp.error().msg("删除失败");
    }




    @ApiOperation("通过id逻辑删除一个用户对象")
    @PostMapping("deleteUserLogicalById")
    public MyRsp deleteUserLogicalById(String uId){
        return userService.deleteUserLogicalById(uId) > 0 ? MyRsp.success("逻辑删除成功"):MyRsp.error().msg("删除失败");
    }


    @ApiOperation("恢复一个被逻辑删除的用户对象 ")
    @PostMapping("recoverUserById")
    public MyRsp recoverUserById(String uId){
        return userService.recoverUserById(uId) > 0 ? MyRsp.success("恢复成功"):MyRsp.error().msg("恢复失败");
    }


    @ApiOperation("根据id来条件修改一个对象")
    @PostMapping("updateUser")
    public MyRsp updateUser(@RequestBody User user){

        return userService.updateUser(user);
    }


    @ApiOperation("根据手机号来判断手机号是否已注册，如果未注册，向手机发送验证码")
    @PostMapping("getYanzhenma")
    public MyRsp getYanzhenma(String username){
       return userService.getYanzhenma(username);
    }

    @ApiOperation("忘记密码时用户重置，数据库有这个用户才进行修改，需要发送验证码")
    @PostMapping("getReg")
    public MyRsp getReg(String username){
      return userService.getReg(username);
    }


    @ApiOperation("抢租客注册")
    @PostMapping("register")
    public MyRsp register(User user,@RequestParam(value = "reg") String reg){
        return userService.register(user, reg);
    }

    @ApiOperation("根据手机号和验证码修改用户密码")
    @PostMapping("updatePass")
    public MyRsp updatePass(User user,@RequestParam(value = "reg") String reg){
        return userService.updatePass(user, reg);
    }


    @ApiOperation("上传用户的身份证，包含身份证图片base64字符串")
    @PostMapping("uploadIdCardImg")
    public MyRsp uploadIdCardImg(@RequestBody String imgstr,@CookieValue(value = "token",defaultValue = "---") String token){
        return userService.uploadIdCardImg(imgstr, token);
    }

    @ApiOperation("用户实名认证，包含用户的真实姓名，还有用户的身份证号码")
    @PostMapping("auth")
    public MyRsp auth(@RequestBody User user,@CookieValue(value = "token",defaultValue = "---") String token){

        return userService.auth(user, token);
    }


    @ApiOperation("根据token获取用户信息")
    @PostMapping("getUserInfo")
    public MyRsp getUserInfo(@CookieValue(value = "token",defaultValue = "---") String token){
        return userService.getUserInfo(token);
    }

    @PostMapping("getUserInfoBytoken")
    public MyRsp getUserInfoBytoken(@RequestBody String token){

        return userService.getUserInfo(token);
    }

    @ApiOperation("用户退出登陆")
    @PostMapping("logout")


    public MyRsp logout(HttpServletResponse response){
        return userService.logout(response);
    }


    @ApiOperation("显示抢租客的信息，对其进行审核")
    @PostMapping("tenantList")
    public Object tenantList(Integer page,Integer rows,String superSearchKeyWord,Integer status,String sort){

        return userService.tenantList(page, rows, superSearchKeyWord, status,sort,3);
    }

    @ApiOperation("显示物业方需要审核的信息")
    @PostMapping("propertyList")
    public Object propertyList(Integer page,Integer rows,String superSearchKeyWord,String sort,Integer status){

        return userService.tenantList(page, rows, superSearchKeyWord, status,sort,1);

    }



    @ApiOperation("显示出租客需要审核的信息")
    @PostMapping("lessorList")
    public Object lessorList(Integer page,Integer rows,String superSearchKeyWord,String sort,Integer status){

        return userService.tenantList(page, rows, superSearchKeyWord, status,sort,2);
    }


    @ApiOperation("修改用户的状态")
    @PostMapping("setUserStatus")
    public MyRsp setUserStatus(@RequestBody User user,@CookieValue(value = "token",defaultValue = "---") String token){

        //修改用户状态
        return userService.setUserStatus(user,token);

    }


    @ApiOperation("添加用户的车牌号码")
    @PostMapping("addCarNumber")
    public MyRsp addCarNumber(@RequestBody User user,@CookieValue(value = "token",defaultValue = "---") String token){

        return userService.addCarNumber(user, token);
    }

    @ApiOperation("对用户进行解锁和锁定")
    @PostMapping("setUserCode")
    public MyRsp setUserCode(@RequestBody User user){

        return userService.setUserCode(user);
    }


    @ApiOperation("对抢租客用户的信息进行管理")
    @PostMapping("tenantManage")
    public Object tenantManage(Integer page,Integer rows,String superSearchKeyWord,Integer code,String pageParam){

        return userService.tenantManage(page, rows, superSearchKeyWord, code,pageParam,3);
    }

    @ApiOperation("对出租客租客用户的信息进行管理")
    @PostMapping("lessorManage")
    public Object lessorManage(Integer page,Integer rows,String superSearchKeyWord,Integer code,String pageParam){

        return userService.tenantManage(page, rows, superSearchKeyWord, code,pageParam,2);
    }

    @ApiOperation("对物业方用户的信息进行管理")
    @PostMapping("properytManage")
    public Object properytManage(Integer page,Integer rows,String superSearchKeyWord,Integer code,String pageParam){

        return userService.tenantManage(page, rows, superSearchKeyWord, code,pageParam,1);
    }


    @ApiOperation("物业方注册")
    @PostMapping("propertyRegister")
    public MyRsp propertyRegister(@RequestBody PropertyParam propertyParam){

        return userService.propertyRegister(propertyParam);
    }


    @ApiOperation("出租方注册")
    @PostMapping("lessorRegister")
    public MyRsp lessorRegister(@RequestBody PropertyParam propertyParam){
     return userService.lessorRegister(propertyParam);
    }

    /*@ApiOperation("根据id查询用户电话")
    @PostMapping("getUserByIdTel")
    public MyRsp getUserByIdTel(@RequestBody User user){

        User userByIdTel = userService.getUserByIdTel(user.getUId());
        if (userByIdTel!=null){
            return MyRsp.success(userByIdTel);
        }
        else {
            return MyRsp.error();
        }
    }*/

}
